// key-value 模型
public class HashBucket {
    private static class Node {
        private int key;
        private int value;
        Node next;


        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }


    private Node[]  array;
    private int size;   // 当前的数据个数
    private static final double LOAD_FACTOR = 0.75;
    private static final int DEFAULT_SIZE = 8;//默认桶的大小

    public HashBucket() {
        this.array = new Node[DEFAULT_SIZE];
    }

    //
    public void put(int key, int value) {
        Node node = new Node(key, value);
        int index = key % array.length;
        Node cur = array[index];
        while(cur != null) {
            if(cur.key == key) {
                cur.value = value;
                return;
            }
            cur = cur.next;
        }
        node.next = array[index];
        array[index] = node;
        size++;
        if(loadFactor() > LOAD_FACTOR) {
            resize();
        }
    }


    private void resize() {
        Node[] tmpArray = new Node[array.length * 2];
        for (int i = 0; i < array.length; i++) {
            while (array[i] != null) {
                Node next = array[i].next;      //保存当前结点的下一个结点
                int newIndex = array[i].key % tmpArray.length;
                array[i].next =tmpArray[newIndex];
                tmpArray[newIndex] = array[i];
                array[i] = next;
            }
        }
        array = tmpArray;
    }


    private double loadFactor() {
        return size * 1.0 / array.length;
    }

    public int get(int key) {
        int index = key % array.length;
        Node cur = array[index];
        while(cur != null) {
            if(cur.key == key) {
                return cur.value;
            }
            cur = cur.next;
        }

        return -99999;
    }
}
